//
//  UIImage+Extension.swift
//  图片选择
//
//  Created by wulei on 15/12/18.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit

extension UIImage {
    func scaleToWidth(width: CGFloat) -> UIImage {
        
        if width > size.width {
            return self
        }
        
        let height = size.height * width / size.width
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        UIGraphicsBeginImageContext(rect.size)
        self.drawInRect(rect)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    
    }
}
